let url_config = "https://testzh.dpcpower.com"; //测试环境
// let url_config = "https://zh.dpcpower.com"; //正式环境
// let url_config ="http://172.16.53.14:8002";
// import {
//   fungoPreviousPage
// } from "@/common/main.js";

function service(options = {}, method, data = {}) {
  let token = uni.getStorageSync('token') || null;
  options.url = `${url_config}${options.url}`;
  //配置请求头
  options.header = {
    'content-type': 'application/json',
    'Authorization': token
  };
  options.method = method;

  options.data = data;
  return new Promise((resolved, rejected) => {
    //成功
    options.success = (res) => {
      let code = res.data.code;
      if (+code == 200) { //请求成功
        resolved(res.data);
      } else if (code == 401) {
        // fungoPreviousPage();
        uni.hideLoading();
        uni.clearStorage();
        uni.clearStorageSync();
        uni.showModal({
          title: '提示',
          content: '暂未授权登录，是否登录？',
          success: function(res) {
            if (res.confirm) {
              uni.reLaunch({
                url: '/pages/login/login'
              })
            }
          }
        })
      } else {
        uni.showToast({
          icon: 'none',
          duration: 4000,
          title: `${res.data.message}`
        })
        rejected(res.data); //错误
      }
    }
    //错误
    options.fail = (err) => {
      if (err.errMsg == 'request:fail timeout') {
        uni.showToast({
          title: '请求超时，请稍后重试!',
          duration: 4000,
          icon: "none"
        });
      } else if (err.errMsg == 'request:fail ') {
        uni.showToast({
          title: '网络异常，请检查网络!',
          duration: 4000,
          icon: "none"
        });
      }
      rejected(err); //错误
    }
    uni.request(options);

  })
}
export default service;
